class Solution 
{
public:
    ListNode* rotateRight(ListNode* head, int k) 
    {
        if (k == 0 || head == nullptr || head->next == nullptr)
            return head;

        int n = 1;
        ListNode* it = head;
        while (it->next != nullptr) 
        {
            it = it->next;
            n++;
        }
        int add = n - k % n;
        if (add == n)
            return head;

        it->next = head;
        while (add--) 
        {
            it = it->next;
        }
        ListNode* ret = it->next;
        it->next = nullptr;
        return ret;
    }
};